﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

using FWF.Logging.Statistics;

using log4net.Core;

using NUnit.Framework;

namespace FWF.Logging.Test.Statistics
{
    /// <summary>
    /// 
    /// </summary>
    [TestFixture]
    public class TimeTakenTest
    {

        private readonly InMemoryLogger _testLog = new InMemoryLogger();

        /// <summary>
        /// Setups this instance.
        /// </summary>
        [SetUp]
        public void Setup()
        {
            _testLog.Reset();
        }

        /// <summary>
        /// Froms the name.
        /// </summary>
        [Test]
        public void HasMessagesFromDispose()
        {
            using ( TimeTaken.Start( _testLog, "Test", "Test TimeTaken #2" ) )
            {

            }

            IEnumerable<string> listMessages = _testLog.GetMessagesForLog( Level.Trace );

            foreach ( string s in listMessages )
            {
                Debug.WriteLine( s );
            }

            var wrappingCollection = new List<string>(listMessages);

            Assert.IsNotEmpty( wrappingCollection );
            Assert.GreaterOrEqual(
                wrappingCollection.Count,
                2
                );
        }

        /// <summary>
        /// Determines whether [has messages from close].
        /// </summary>
        [Test]
        public void HasMessagesFromClose()
        {
            TimeTaken tt = TimeTaken.Start( _testLog, "Test", "Test TimeTaken #2" );
            tt.Close();

            IEnumerable<string> listMessages = _testLog.GetMessagesForLog( Level.Trace );

            foreach ( string s in listMessages )
            {
                Debug.WriteLine( s );
            }

            var wrappingCollection = new List<string>( listMessages );

            Assert.IsNotEmpty( wrappingCollection );
            Assert.GreaterOrEqual(
                wrappingCollection.Count,
                2
                );
        }

        /// <summary>
        /// Adds the message.
        /// </summary>
        [Test]
        public void AddMessageFromClose()
        {
            TimeTaken tt = TimeTaken.Start( _testLog, "Test", "Test TimeTaken #1" );
            tt.AddMessage( "This is a test message" );
            tt.Close();

            IEnumerable<string> listMessages = _testLog.GetMessagesForLog( Level.Trace );

            foreach ( string s in listMessages )
            {
                Debug.WriteLine( s );
            }

            var wrappingCollection = new List<string>( listMessages );

            Assert.IsNotEmpty( wrappingCollection );
            Assert.GreaterOrEqual(
                wrappingCollection.Count,
                3
                );
        }

        /// <summary>
        /// Adds the message.
        /// </summary>
        [Test]
        public void AddMessageFromDispose()
        {
            using ( TimeTaken tt = TimeTaken.Start( _testLog, "Test", "Test TimeTaken #1" ) )
            {
                tt.AddMessage( "This is a test message" );
            }

            IEnumerable<string> listMessages = _testLog.GetMessagesForLog( Level.Trace );

            foreach ( string s in listMessages )
            {
                Debug.WriteLine( s );
            }

            var wrappingCollection = new List<string>( listMessages );

            Assert.IsNotEmpty( wrappingCollection );
            Assert.GreaterOrEqual(
                wrappingCollection.Count,
                3
                );
        }


    }
}
